package com.bridge.applets.service.impl;

import com.bridge.applets.model.entity.BridgeMeasureModel;
import com.bridge.applets.model.enums.SampleTypeEnum;
import com.bridge.applets.model.vo.req.BridgeMeasureFileSerialNumReq;
import com.bridge.applets.model.vo.req.BridgeMeasureModelNumReq;
import com.bridge.applets.model.vo.resp.BridgeMeasureModelNumResp;
import com.bridge.applets.model.vo.resp.BridgeMeasureModelSerialNumResp;
import com.bridge.applets.service.*;
import com.bridge.applets.strategy.factory.BridgeFactory;
import com.bridge.applets.strategy.factory.FunctionModelFactory;
import com.bridge.applets.utils.AliyunOSSUtil;
import com.bridge.applets.utils.FileUploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Slf4j
@Service
public class BridgeMobileServiceImpl implements BridgeMobileService {

    @Autowired
    private BridgeFactory bridgeFactory;

    @Autowired
    private FunctionModelFactory functionModelFactory;
    @Autowired
    private BridgeNeuralNetworkService bridgeNeuralNetworkService;
    @Autowired
    private BridgeIdentifyCaeService bridgeIdentifyCaeService;
    @Autowired
    private BridgeMonitorAnalysisService bridgeMonitorAnalysisService;

    @Autowired
    private BridgeRecordService bridgeRecordService;

    @Autowired
    private BridgeMeasureModelService bridgeMeasureModelService;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private FileUploadUtil fileUploadUtil;

    @Override
    public void addMeasureModel(MultipartFile file, String fileName, String bridgeName, String bridgeCode, Integer sampleType,String platform) {
        /**
         * 1:调用数据校验 模块，校验数据是否没问题
         * 2：正常保存数据
         */

        //上传，然后更新视频文件
        String filePath = AliyunOSSUtil.PRE_PATH_FILE_CSV + "/" + bridgeCode + "/"+platform+"/";
        // String url = aliyunOSSUtil.upLoadByName(file, filePathName);
        String url = fileUploadUtil.uploadByName(file, filePath, fileName);
        BridgeMeasureModel entity = new BridgeMeasureModel();
        entity.setBridgeName(bridgeName);
        entity.setBridgeCode(bridgeCode);
        entity.setFileName(fileName);
        entity.setFileUrl(url);
        entity.setSampleType(sampleType);
        //保存文件
        bridgeMeasureModelService.save(entity);
    }

    @Override
    public BridgeMeasureModelNumResp getMeasureModelNum(BridgeMeasureModelNumReq req) {
        Map<Integer, Integer> sampleTypeCountMap = bridgeMeasureModelService.queryCountSampleType(req.getBridgeCode());
        BridgeMeasureModelNumResp resp = new BridgeMeasureModelNumResp();
        resp.setNumType0(sampleTypeCountMap.getOrDefault(SampleTypeEnum.TYPE_0.getType(), 0));
        resp.setNumType1(sampleTypeCountMap.getOrDefault(SampleTypeEnum.TYPE_1.getType(), 0));
        return resp;
    }

    @Override
    public BridgeMeasureModelSerialNumResp getMeasureFileSerialNum(BridgeMeasureFileSerialNumReq req) {
        Map<Integer, Integer> sampleType = bridgeMeasureModelService.queryCountSampleType(req.getBridgeCode());
        BridgeMeasureModelSerialNumResp resp = new BridgeMeasureModelSerialNumResp();
        resp.setSerialNum(sampleType.getOrDefault(req.getSampleType(), 0));
        return resp;
    }

    @Override
    public void uploadFile(MultipartFile file, String fileName) {
        String url = fileUploadUtil.uploadByName(file, "apk/", fileName);

        log.info("uploadFile,url:{}", url);
    }
}
